#include "mousehit.h"
#include "ui_mousehit.h"
#include <QCloseEvent>
#include <QMessageBox>
#include <QDebug>

MouseHit::MouseHit(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MouseHit)
{
    ui->setupUi(this);
    ui->btnStart->setEnabled(true);
    ui->btnStop->setEnabled(false);
    ui->btnReset->setEnabled(false);

    ms = new mouseScene();
    ui->mouseHitView->setScene(ms);
    hm = HandlerMouse::getInstance();
    connect(hm, SIGNAL(scoreSignal()), this, SLOT(addScore()));
    score = 0;
}

MouseHit::~MouseHit()
{
    delete ui;
}

void MouseHit::closeEvent(QCloseEvent *event)
{
    int ret = QMessageBox::question(this, "Exit", "Are you sure to exit?");
    if (ret == QMessageBox::No) {
        event->ignore();
    }
}

void MouseHit::resizeEvent(QResizeEvent *event)
{
    qDebug() << ui->mouseHitView->width() << " " << ui->mouseHitView->height();
}

void MouseHit::addScore()
{
    score += 1;
    ui->lcdNumber->display(score);
    if(score>=50){
        ui->btnStart->setText("你的每场对局");
        ui->btnReset->setText("都没有水下小人");
        ms->timer->stop();
        ui->lcdNumber->display("99999");
        ui->btnStop->setText("胜率节节高");
        ms->setMousePause();
    }
}

void MouseHit::on_pushButton_clicked()
{

    // TODO: Implement this function
}

void MouseHit::on_btnStart_clicked()
{
    ms->timer->start(1500);
    ui->btnStart->setEnabled(false);
    ui->btnStop->setEnabled(true);
    ui->btnReset->setEnabled(true);
}

void MouseHit::on_btnStop_clicked()
{
    if (ui->btnStop->text() == "Pause") {
        ms->timer->stop();
        ui->btnStop->setText("Continue");
        ms->setMousePause();
    } else {
        ui->btnStop->setText("Pause");
        ms->timer->start(1500);
        ms->setMouseActive();
    }
}

void MouseHit::on_btnReset_clicked()
{
    ui->btnStart->setEnabled(true);
    ui->btnStop->setEnabled(false);
    ms->timer->stop();
    score = 0;
    ui->lcdNumber->display(score);
    ui->btnStop->setText("Pause");
    ms->setMousePause();
}
